package laborum2011.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import laborum2011.dao.ContactoDAO;
import laborum2011.dao.EmpresaDAO;
import laborum2011.dao.OfertaDAO;
import laborum2011.dao.PersonaDAO;
import laborum2011.excepcion.DAOExcepcion;
import laborum2011.modelo.Contacto;
import laborum2011.modelo.Empresa;
import laborum2011.modelo.Loguin;
import laborum2011.modelo.Oferta;
import laborum2011.modelo.Persona;

/**
 * Servlet implementation class MainAnunServlet
 */
@WebServlet("/MainAnunServlet")
public class MainAnunServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainAnunServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.procesarPeticion(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.procesarPeticion(request, response);
	}

	protected void procesarPeticion(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("Dentro de doPost del servlet BuscaOfertas");

		HttpSession session = request.getSession();

		PersonaDAO pDAO = new PersonaDAO();
		ContactoDAO cDAO = new ContactoDAO();
		EmpresaDAO eDAO = new EmpresaDAO();
		
		Loguin logueo = new Loguin();
		Persona persona = new Persona();
		Contacto contacto = new Contacto();
		Empresa empresa = new Empresa();

		logueo = (Loguin) session.getAttribute("logueo");
			
		System.out.println("idUsuario 0 " + logueo.getId_usuario());
		
		try {
			persona = pDAO.buscarXId(logueo.getId_usuario());
			contacto = cDAO.obtener(persona.getId_persona());
			empresa = eDAO.obtenerPorIdContacto(contacto.getId_contacto());
		} catch (DAOExcepcion e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		List<Oferta> ofertas = new ArrayList<Oferta>();
		OfertaDAO ODao = new OfertaDAO();

		try {
			ofertas = ODao.obtenerOfertaxEmpresa(empresa.getId_empresa());
			request.setAttribute("ofertas", ofertas);
			RequestDispatcher rd = request.getRequestDispatcher("MainAnunciante.jsp");
			rd.forward(request, response);

		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			//RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			//rd.forward(request, response);
		}
	}
}
